package zone.haiwen.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import zone.haiwen.demo.domain.dao.MyuserMapper;
import zone.haiwen.demo.domain.entity.vo.Myuser;
import zone.haiwen.demo.service.IMyuserService;
import zone.haiwen.demo.util.RedisUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * Description: 服务实现类
 * @author zhangHaiWen
 * @data 2021/7/7 19:55
 * @return
 **/
@Service
public class MyuserServiceImpl extends ServiceImpl<MyuserMapper, Myuser> implements IMyuserService {
    protected static Logger logger = LoggerFactory.getLogger(MyuserServiceImpl.class);

	@Resource
	private RedisTemplate<String, Object> redisTemplate;

	/**
	 * Description:缓存逻辑：如果缓存存在，则从缓存中获取用户信息;否则，则从db中获取用户信息，然后插入到缓存
	 * @author zhangHaiWen
	 * @data 2021/7/7 16:32
	 * @return haiwen.zone.demo.domain.entity.vo.Myuser
	 **/
	@Override
	public Myuser getUserByName(String name) {
		logger.debug("访问get:key name={}", name);
	   Myuser myuser = (Myuser) RedisUtil.get(this.redisTemplate, "person:" + name);
	   if (myuser != null) {
	   	String result = myuser.toString();
	   	logger.debug("get obj result=" + result);
	   	return myuser;
	   }
	   Myuser dbResult = this.baseMapper.getUserByName(name);
	   logger.debug("访问set: " + dbResult.toString());
	   RedisUtil.set(this.redisTemplate, "person:" + dbResult.getName(), dbResult);
	   return dbResult;
	}

	/**
	 * Description
	 * @author zhangHaiWen
	 * @data 2021/7/7 16:32
	 * @return java.util.List<haiwen.zone.demo.domain.entity.vo.Myuser>
	 **/
	@Override
	public List<Myuser> getUserByAge(int age) {
		return this.baseMapper.getUserByAge(age);
	}

	/**
	 * Description
	 * @author zhangHaiWen
	 * @data 2021/7/7 16:32
	 * @return void
	 **/
	@Override
	public void insertUser(Long id, String name, Integer age, String email) {
		this.baseMapper.insertUser(id, name, age, email);
	}

	/**
	 * Description
	 * @author zhangHaiWen
	 * @data 2021/7/7 16:32
	 * @return void
	 **/
	@Override
	public void updateUser(Long id, String name, Integer age, String email) {
		this.baseMapper.updateUser(id, name, age, email);
	}

	/**
	 * Description
	 * @author zhangHaiWen
	 * @data 2021/7/7 16:31
	 * @return void
	 **/
	@Override
	public void deleteUserById(Long id) {
		this.baseMapper.deleteUserById(id);
	}

	/**
	 * Description: if条件判断
	 * @author zhangHaiWen
	 * @data 2021/7/7 16:34
	 * @return haiwen.zone.demo.domain.entity.vo.Myuser
	 **/
	@Override
	public Myuser queryUserIf(Long id, String name) {
		return this.baseMapper.queryUserIf(id, name);
	}

	/**
	 * Description: Choose判断
	 * @author zhangHaiWen
	 * @data 2021/7/7 16:34
	 * @return java.util.List<haiwen.zone.demo.domain.entity.vo.Myuser>
	 **/
	@Override
	public List<Myuser> queryUserChoose(String name, Integer age) {
		return this.baseMapper.queryUserChoose(name, age);
	}

	/**
	 * Description: where和if条件判断
	 * @author zhangHaiWen
	 * @date 2021/7/7 16:34
	 * @return java.util.List<haiwen.zone.demo.domain.entity.vo.Myuser>
	 **/
	@Override
	public List<Myuser> queryUserWhere(String name, Integer age) {
	    return this.baseMapper.queryUserWhere(name, age);
	}

	/**
	 * Description: 遍历
	 * @author zhangHaiWen
	 * @data 2021/7/7 19:58
	 * @return java.util.List<haiwen.zone.demo.domain.entity.vo.Myuser>
	 **/
	@Override
		public List<Myuser> queryUserForeach(List<Long> list) {
		return this.baseMapper.queryUserForeach(list);
	}

	@Override
	public Myuser getUserById(Integer id) {
		return this.baseMapper.getUserById(id);
	}
}
